<template>
    <div>
        <div>
            <el-input
                size="small"
                class="addJobInput"
                placeholder="添加职位..."
                suffix-icon="el-icon-plus"
                @keydown.enter.native="addJob"
                v-model="job.name">
            </el-input>
            <el-button size="small" icon="el-icon-plus" type="primary" @click="addJob">添加</el-button>
        </div>
        <div class="jobMain">
            <el-table
                stripe
                border
                size="small"
                type="selection"
                :data="tableJob"
                @selection-change="handleSelectionChange"
                style="width: 70%">
                <el-table-column
                    type="selection"
                    width="55">
                </el-table-column>
                <el-table-column
                    prop="id"
                    label="编号"
                    width="55">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="职位"
                    width="120">
                </el-table-column>
                <el-table-column
                    prop="createDate"
                    label="创建时间"
                    width="200">
                </el-table-column>
                <el-table-column
                    label="操作"
                    width="200">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            @click="showEditView(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-button size="small" type="danger" @click="deleteMany" :disabled="this.multipleSelection.length == 0">批量删除</el-button>
        <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="30%">
            <div>
                <el-tag>职位名称</el-tag>
                <el-input size="small" v-model="updateJob.name" class="updateJobInput"></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
    <el-button size="small" @click="dialogVisible = false">取 消</el-button>
    <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
  </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: 'JobManager',
        data () {
            return {
                job: {
                    name: ''
                },
                tableJob: [],
                dialogVisible: false,
                updateJob: {
                    name: ''
                },
                multipleSelection: []
            }
        },
        mounted () {
            this.initTableJob()
        },
        methods: {
            initTableJob () {
                this.getRequest('/system/basic/job').then((resp) => {
                    this.tableJob = resp
                    this.job.name = ''
                })
            },
            addJob () {
                if (this.job.name) {
                    this.postRequest('/system/basic/addJob', this.job).then((resp) => {
                        this.initTableJob()
                    })
                } else {
                    this.$message.error('职位不能为空')
                }
            },
            showEditView (index, data) {
                Object.assign(this.updateJob, data)  //拷贝，防止同步显示，this.updateJob = data会同时显示修改
                this.dialogVisible = true
            },
            doUpdate () {
                this.putRequest('/system/basic/updateJob', this.updateJob).then(resp => {
                    this.initTableJob()
                    this.dialogVisible = false
                })
            },
            handleDelete (index, data) {
                this.$confirm('此操作将永久删除[' + data.name + ']职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/deleteJob?ids=' + data.id).then(resp => {
                        this.initTableJob()

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            },
            handleSelectionChange (val) {
                this.multipleSelection = val
            },
            deleteMany () {
                this.$confirm('此操作将永久删除' + this.multipleSelection.length + '个职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?'
                    this.multipleSelection.forEach(value => {
                        ids = ids + 'ids=' + value.id + '&'
                    })
                    ids = ids.substr(0, ids.length - 1)
                    this.deleteRequest('/system/basic/deleteJob' + ids).then(resp => {
                        this.initTableJob()

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            }
        }
    }
</script>

<style scoped>
    .addJobInput {
        width: 300px;
        margin-right: 8px;
    }

    .jobMain {
        margin-top: 10px;
    }

    .updateJobInput {
        width: 200px;
        margin-left: 8px;
    }
</style>
